package com.warehouse.service;

import com.warehouse.dao.UserDao;
import com.warehouse.dao.impl.UserDaoImpl;
import com.warehouse.entity.Cargo;
import com.warehouse.entity.User;
import com.warehouse.utils.Page;

import java.util.List;

public class UserService {
    private UserDao userDao=new UserDaoImpl();
    public Page<User> queryPage(int start, int pageSize) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = userDao.count();//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<User> list = userDao.allPage(startPage, pageSize);
        return new Page<User>(start, totalPage, pageSize, list);
    }

    public int add(User user) {
        return userDao.add(user);
    }

    public int update(User user) {
        return userDao.update(user);
    }


    public int delete(int id) {
        return userDao.delete(id);
    }

    public User byId(int id) {
        return userDao.byId(id);
    }

    public Page<User> byCardNum(int start, int pageSize, String name) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = userDao.countByAdminNum(name);//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<User> cargos = userDao.byAdminNum(startPage, pageSize, name);
        return new Page<User>(start, totalPage, pageSize, cargos);
    }

}
